﻿using System;
using System.Collections.Generic;
using System.Linq;
#if !NETCOREAPP
using System.Web;
using System.Web.Mvc;
using System.Web.WebPages;
#endif

#if NETCOREAPP
using Microsoft.AspNetCore.Mvc.Rendering;
using Kugar.Core.Web;
#endif

using Kugar.Core.ExtMethod;
using Kugar.MVC.MobileUI.WeUI.Enum;
using Kugar.MVC.MobileUI.WeUI.Helpers;

namespace Kugar.MVC.MobileUI.WeUI.PageControls.PageForm
{
    /// <summary>
    /// 时间/时间日期选择框
    /// </summary>
    public class PageForm_DatePicker:PageForm_FieldBase
    {
        private DatePickerTypeEnum _type = DatePickerTypeEnum.Date;
        private DateTime? _value = null;
        private bool _isReadonly = false;
        private string _onchangescript = "";
        private DateTime? _maxValue = null;
        private DateTime? _minValue = null;

        public PageForm_DatePicker(ViewContext viewContext) : base(viewContext)
        {
            
        }

        public PageForm_DatePicker Type(DatePickerTypeEnum type)
        {
            _type = type;

            return this;
        }

        public PageForm_DatePicker Value(DateTime? value)
        {
            _value = value;

            return this;
        }

        public PageForm_DatePicker ReadOnley(bool isReadonley)
        {
            _isReadonly = isReadonley;

            return this;
        }

        public PageForm_DatePicker Max(DateTime? value)
        {
            _maxValue = value;

            return this;
        }

        public PageForm_DatePicker Min(DateTime? value)
        {
            _minValue = value;

            return this;
        }

        public PageForm_DatePicker OnChange(string onchangescript)
        {
            _onchangescript = onchangescript;

            return this;
        }

        protected override void RenderHeader()
        {
            ViewContext.Writer.WriteLine($"<label  class=\"weui-label\">{_label}</label>");
        }

        protected override void BeginRender()
        {
            switch (_type)
            {
                case DatePickerTypeEnum.Date:
                    this.Css("datepicker");
                    break;
                case DatePickerTypeEnum.Datetime:
                    this.Css("datetimepicker");
                    break;
                case DatePickerTypeEnum.Time:
                    this.Css("timepicker");
                    break;
            }

            base.BeginRender();

        }

        protected override void RenderBody()
        {
            switch (_type)
            {
                case DatePickerTypeEnum.Date:
                    renderDatePicker();
                    break;
                case DatePickerTypeEnum.Datetime:
                    renderDateTimePicker();
                    break;
                case DatePickerTypeEnum.Time:
                    renderTimePicker();
                    break;
            }
        }

        private void renderDatePicker()
        {
            

            ViewContext.Writer.WriteLine($"<input class=\"weui-input\" data-max=\"{_maxValue.ToStringEx("yyyy-MM-dd")}\" data-min=\"{_minValue.ToStringEx("yyyy-MM-dd")}\" data-onchange='{_onchangescript}' name=\"{_name}\" value=\"{_value.ToStringEx("yyyy-MM-dd")}\" type=\"text\" {_isReadonly.If("readonly")} />");
        }

        private void renderDateTimePicker()
        {
            

            ViewContext.Writer.WriteLine($"<input class=\"weui-input\" data-max=\"{_maxValue.ToStringEx("yyyy-MM-dd")}\" data-min=\"{_minValue.ToStringEx("yyyy-MM-dd")}\" data-onchange='{_onchangescript}' name=\"{_name}\" value=\"{_value.ToStringEx("yyyy-MM-dd HH:mm")}\" type=\"text\" {_isReadonly.If("readonly")} />");
        }

        private void renderTimePicker()
        {
            

            ViewContext.Writer.WriteLine($"<input class=\"weui-input\" data-max=\"{_maxValue.ToStringEx("yyyy-MM-dd")}\" data-min=\"{_minValue.ToStringEx("yyyy-MM-dd")}\" data-onchange='{_onchangescript}' name=\"{_name}\" value=\"{_value.ToStringEx("HH:mm")}\" type=\"text\" {_isReadonly.If("readonly")} />");
        }
    }
}